package com.cwvs.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.cwvs.models.AppFunction;

/**
 * 
 * 功能菜单DAO
 */
@Repository
public class AppFunctionDao {
	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 获取根目录菜单
	 * 
	 * @return
	 */
	public AppFunction getRoot() {
		StringBuffer sbf = new StringBuffer();
		sbf.append("  select * from app_function where parent_id=0 or parent_id is null ");
		List<AppFunction> ls = jdbcTemplate.query(sbf.toString(),
				new RowMapper<AppFunction>() {
					@Override
					public AppFunction mapRow(ResultSet r, int i)
							throws SQLException {
						AppFunction o = new AppFunction();
						o.setId(r.getString("id"));
						o.setCode(r.getString("code"));
						o.setName(r.getString("name"));
						o.setUrl(r.getString("url"));
						o.setParentId(r.getString("parent_id"));
						o.setLft(r.getString("lft"));
						o.setRgt(r.getString("rgt"));
						o.setHandler(r.getString("handler"));
						o.setHandlerUrl(r.getString("handler_url"));
						o.setHandlerMethod(r.getString("handler_method"));
						o.setTheType(r.getString("the_type"));
						o.setNeedAuth(r.getBoolean("need_auth"));
						o.setIsMenu(r.getBoolean("is_menu"));
						o.setIsFunctionMenu(r.getBoolean("is_function_menu"));
						o.setActive(r.getBoolean("is_active"));
						return o;
					}
				});
		if (ls.isEmpty()) {
			return new AppFunction();
		}
		return ls.get(0);
	}
	/**
	 * 通过上级ID获取下级
	 * @param parent_id
	 * @return
	 */
	public List<AppFunction> findAppFunctionsByParent(String parent_id) {
		return jdbcTemplate
				.query(" select * from app_function where parent_id=? and is_active=1",
						new Object[] { parent_id },
						new RowMapper<AppFunction>() {
							@Override
							public AppFunction mapRow(ResultSet r, int i)
									throws SQLException {
								AppFunction o = new AppFunction();
								o.setId(r.getString("id"));
								o.setCode(r.getString("code"));
								o.setName(r.getString("name"));
								o.setUrl(r.getString("url"));
								o.setParentId(r.getString("parent_id"));
								o.setLft(r.getString("lft"));
								o.setRgt(r.getString("rgt"));
								o.setHandler(r.getString("handler"));
								o.setHandlerUrl(r.getString("handler_url"));
								o.setHandlerMethod(r
										.getString("handler_method"));
								o.setTheType(r.getString("the_type"));
								o.setNeedAuth(r.getBoolean("need_auth"));
								o.setIsMenu(r.getBoolean("is_menu"));
								o.setIsFunctionMenu(r
										.getBoolean("is_function_menu"));
								o.setActive(r.getBoolean("is_active"));
								return o;
							}
						});
	}
	
    public List<String> allowFunctions(String user_id, String user_name) {
        String sql = "select d.id from app_user a left outer join app_user_role b on a.id = b.user_id " +
        		"left outer join app_role_auth c on b.role_id = c.role_id left outer join app_function d on c.function_id = d.id " +
        		"where a.id=?";
        List<String> params=new ArrayList<String>();
        if(user_name.equalsIgnoreCase("admin")){
            sql = "select id from app_function";
        }else{
        	params.add(user_id);
        }
        List<String> list = new ArrayList<String>();
        try{
            list = jdbcTemplate.queryForList(sql,params.toArray(), String.class);
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
